public class Thread_l implements Runnable {
    protected int SIZE = 20000; // 棋盘大小
    protected int LINE = 0; // 代数

    protected int [][] board= new int[SIZE][SIZE];

    public LifeChess lifeChess;

    public void run(){
        nextGeneration(this.board);
    }

    public void setBoard(int[][] board){
        this.board = board;
    }

    public void setSIZE(int SIZE){
         this.SIZE = SIZE;
    }

    public void setLINE(int LINE){
        this.LINE = LINE;
    }

    public Thread_l(LifeChess lifeChess){
        this.lifeChess = lifeChess;
    }

    public LifeChess getLifeChess(){
        return lifeChess;
    }

    public void setLifeChess(LifeChess lifeChess){
        this.lifeChess = lifeChess;
    }

    public void nextGeneration(int[][] mode) {
        int[][] newBoard = new int[SIZE][SIZE];
        if(LINE > 0){
            for(int i = LINE-1; i <= LINE+1; i++){
                System.arraycopy(mode[i], 0, newBoard[i], 0, SIZE);
            }
        }else{
            for(int i = LINE; i <= LINE+1; i++){
                System.arraycopy(mode[i], 0, newBoard[i], 0, SIZE);
            }
        }

        //迭代
                for (int j = 0; j < this.SIZE; j++) {
                    int alivenbCells = countAliveNeighbors(newBoard, 1, j);

                    if (newBoard[1][j] == 1) {
                        // 规则1和规则2
                        lifeChess.board[LINE][j] = (alivenbCells == 2 || alivenbCells == 3) ? 1 : 0;
                    } else {
                        // 规则3
                        lifeChess.board[LINE][j] = (alivenbCells == 3) ? 1 : 0;
                    }
                }

    }
    public int countAliveNeighbors(int[][] board, int x, int y) {
        int count = 0;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i == 0 && j == 0) continue; // 自身不算
                int nx = x + i;
                int ny = y + j;
                if (nx >= 0 && nx < 3 && ny >= 0 && ny < SIZE) {
                    count += board[nx][ny];
                }
            }
        }
        return count;
    }
}
